﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Net;
using FluentScheduler;
using equinoxWeb.Models;
using equinoxWeb.GestionConexion;
using System.IO;
namespace equinoxWeb.Controllers
{
    public class MailController : Controller , ITask
    {
        //
        // GET: /Mail/
        private string address;
        private string subject;
        private string message;
        private static string email = "noreply.equinox@gmail.com";
        private static string password = "jhepgmmvkwntoheb";
        private static string server = "smtp.gmail.com";
        private static int port =587;

        public MailController()
        {
        }

        public MailController(string address, string subject, string message)
        {
            this.address = address;
            this.subject = subject;
            this.message = message;
        }
       
        public static void SendEmail(string address, string subject, string message)
        {

            var loginInfo = new NetworkCredential(MailController.email, MailController.password);
            var msg = new MailMessage();
            var smtpClient = new SmtpClient(MailController.server, MailController.port);

            msg.From = new MailAddress(email);
            msg.To.Add(new MailAddress(address));
            msg.Subject = subject;
            msg.Body = message;
            msg.IsBodyHtml = true;

            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = loginInfo;
            smtpClient.Send(msg);
        }

        public List<String> loadParameters()
        {
            //StreamReader sr = new StreamReader(HttpContext.Server.MapPath("..")+"mail.cfg");
            MailController.email = "noreply.equinox@gmail.com";//sr.ReadLine();
            MailController.password = "jhepgmmvkwntoheb";// sr.ReadLine();
            MailController.server = "smtp.gmail.com";//sr.ReadLine();
            MailController.port = 587;//Int32.Parse(sr.ReadLine());

            List<String> correos = new List<string>();
            correos.Add("operations@equinox.pe");
            correos.Add("equinox@equinox.pe");
            /*while (!sr.EndOfStream)
            {
                correos.Add(sr.ReadLine());
            }
            sr.Close();*/
            return correos;
        }

        public void Execute()
        {
            List<String> correos = loadParameters();
            List<Caso> listaCasos = CasoFunction.listarCasosSinNotificar();
            foreach (Caso caso in listaCasos)
            {
                string cuerpo = "Se le recuerda crear el reporte preliminar del caso " + CasoFunction.obtenerReferencia(caso.IdCaso);
                string asunto = "Recordatorio de reporte preliminar";

                foreach (String correo in correos)
                {
                    SendEmail(correo, asunto, cuerpo);
                }
                CasoFunction.marcarComoNotificado(caso.IdCaso,1);
            }
        }
    }
}
